<?php
/**
 * Модель ленты плоских комментариев. 
 *
 * @package default
 * @author Alexander Sibiryakov
 **/
class Model_FlatComments extends RL_Model2
{
	private $tdis_enum;
	private $count;
	private $poster_oid;
	
	/**
	 * Посчитает кол-во комментариев
	 *
	 * @return int
	 * @param $item_oid
	 * @author Alexander Sibiryakov
	 **/
	public function commentsCount( $item_oid ) {
		$result = $this->store->find( 'select count where comment.is_deleted==0 and comment.item_oid='.$item_oid );
		$commentscount = $result->next();
		return $commentscount->data['count'];
	}
	
	/**
	 * Вернуть структуру с плоскими комментариями
	 *
	 * @return array
	 * @param $item_oid - int, обязателен
	 * @param $tdis - array(int) перечисление oid разделов
	 * @param $count - int			
	 * @param $poster_oid - int	
	 * @param $community_oid - int	
	 * @author Alexander Sibiryakov
	 **/	
	public function getCommentsWithoutTree( $item_oid=NULL, array $tdis=NULL, $count=NULL, $poster_oid=NULL, $community_oid=NULL ) {
		$this->tdis_enum = $tdis;
		$this->count = $count;
		$this->poster_oid = $poster_oid;
		$this->community_oid = $community_oid;
		
		return $this->getManyCommentsAsObjects( $item_oid );
	}
	
	private function getManyCommentsAsObjects( $item_oid ) {
		// поиск комментариев к итему
		$db = $this->store->db;
		$prefix = $db->db_prefix;

		if ($item_oid) $item_oid_sql = $db->quote($item_oid);
		if ($this->poster_oid) $poster_oid_sql = $db->quote($this->poster_oid);
		if ($this->tdis_enum) $tdis_enum_sql = $db->quote($this->tdis_enum);
		if ($this->count) $count = $db->quote($this->count);
		if ($this->community_oid) $community_oid_sql = $db->quote($this->community_oid);

		$sql = " SELECT DISTINCT 
				{$prefix}objects.*, 
				afeed.oid as post_oid,
				afeed.title as post_title,
				usr.fullname as user_fullname
				FROM {$prefix}objects"
			    ." INNER JOIN {$prefix}class_comment ON ({$prefix}objects.oid = {$prefix}class_comment.oid)"
			    ." INNER JOIN {$prefix}class_allfeed afeed ON ({$prefix}class_comment.item_oid = afeed.oid)"
				." LEFT OUTER JOIN {$prefix}class_user usr ON usr.oid={$prefix}class_comment.posted_by_id"
				." LEFT OUTER JOIN t_acl_tree tat ON tat.id_object = store_objects.oid"
				//." LEFT OUTER JOIN t_acl_tree tat2 ON tat2.id = tat.parent_id"
			    ." WHERE {$prefix}class_comment.is_deleted = 0"
			    .($item_oid_sql   ? " AND {$prefix}class_comment.item_oid = {$item_oid_sql}":'')
			    .($poster_oid_sql ? " AND {$prefix}class_comment.posted_by_id = {$poster_oid_sql}":'')
			    .($tdis_enum_sql  ? " AND afeed.tdi_oid IN ({$tdis_enum_sql})":'')
			    .($community_oid_sql  ? " AND afeed.community_oid = {$community_oid_sql}":'')
			    ." ORDER BY {$prefix}objects.ctime desc LIMIT ".($count ? $count : 15);
		
		$rs2 = $this->store->findSQL($sql);
		
		// приводим к нужной структуре данных
		if ($rs2)
		while ( $comment = $rs2->next() ) {
			$row = $rs2->rs->current();
			$comment->data = array_merge($comment->data, $row);
			$comment->data['ctime'] = RL_CyrDate::newFromStr("%Y-%m-%d %H:%i:%s", $comment->data['ctime']);
			$comment->data['mtime'] = RL_CyrDate::newFromStr("%Y-%m-%d %H:%i:%s", $comment->data['mtime']);			
			$comments[] = $comment->data;
		}
		return $comments; 
	}	
	
}
